﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MapinfoWrapper.Mapinfo;

namespace FloatingWindowTools.FloatingMapbasicWindow
{
    public partial class MapbasicWindowUI : Form
    {
        private MapinfoSession mapinfo;
        public MapbasicWindowUI(Window mapbasicWindow, MapinfoSession mapInfo)
        {
            InitializeComponent();
            this.mapinfo = mapInfo;
            this.MapbasicWindow = mapbasicWindow;
            this.Load += new EventHandler(MapbasicWindow_Load);
            this.FormClosed += new FormClosedEventHandler(MapbasicWindow_FormClosed);
            this.Resize += new EventHandler(MapbasicWindow_Resize);
        }

        public Window MapbasicWindow { get; set; }

        void MapbasicWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.MapbasicWindow.Close();
        }

        void MapbasicWindow_Load(object sender, EventArgs e)
        {
            Window mapinfoWindow = new Window(this.mapinfo, (int)Window.WindowType.WIN_MAPINFO);

            IntPtr mapinfoID = WinAPI.GetWindowThreadProcessId(this.MapbasicWindow.Handle, new IntPtr(0));
            IntPtr thread = new IntPtr(AppDomain.GetCurrentThreadId());

            // Attach our .net thread to the thread of the window we reparented.
            // This fixs issue (http://code.google.com/p/nathansmapinfoprojects/issues/detail?id=1) which was causing MapInfo's dialogs to draw incorrectly.
            WinAPI.AttachThreadInput(thread, mapinfoID, 1);

            WinAPI.SetParent(this.Handle, mapinfoWindow.Handle);

            //Set the parent of Mpabsic window to a picture box on the form.
            this.MapbasicWindow.Parent = this.pictureBox1.Handle;
            this.MapbasicWindow.RemoveCaptionBar();
            this.MapbasicWindow.RemoveBorder();
            this.MapbasicWindow.Maximize();

            //Add one to the width to update the control.
            this.Width += 1;

            this.mapinfo.Do("Alter Button ID 2005 Disable");
            //MapInfo.MiPro.Interop.InteropServices.MapInfoApplication.Do("Alter Menu Item Load Disable");
        }

        private void MapbasicWindow_Resize(object sender, EventArgs e)
        {
            if (this.MapbasicWindow != null)
            {
                this.MapbasicWindow.UpdateSize(this.pictureBox1.Width, this.pictureBox1.Height);
            }
        }

        public enum WindowStyles : uint
        {
            WS_POPUP = 0x80000000
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams createParams = base.CreateParams;

                // As a top-level window it has no parent
                //createParams.Parent = IntPtr.Zero;

                // Appear as a pop-up window
                int style = createParams.Style;
                createParams.Style = style + unchecked((int)WindowStyles.WS_POPUP);

                return createParams;
            }
        }
    }
}
